<template>
  <div style="height: 100%;">
    <div style="width: 200px;height: 100%;margin: 0 auto;position: relative;">
      <div style="position: absolute;top: 50%;height: 100px;margin-top: -50px;text-align: center;">
        <div><img :src="profilePath" alt=""></div>
        <p>{{nickname}}</p>
        <p>{{text}}</p>
      </div>
    </div>
  </div>
</template>

<script>
// import loginData from '../api/index'
import MenuUtils from '../utils/MenuUtils'
import setChildren from '../utils/setChildren'
import componentData from '../utils/routerPath'
let routers = []
export default {
  name: 'loading',
  data () {
    return {
      profilePath: '',
      nickname: '',
      text: '正在登录'
    }
  },
  methods: {
    loginIn (data) {
      window.sessionStorage.setItem('user', JSON.stringify(data))
      MenuUtils(routers, data)
    },
    handleSubmit (data) {
      // let data = loginData.data
      this.loginIn(data)
      this.$router.addRoutes(routers)
    }
  },
  created () {
    this.$ajax.get('/admin/user/info').then(res => {
      if (res.data.success) {
        let routerData = [{
          path: '/manage',
          name: 'manage',
          component: 'manage',
          children: [
            {
              path: '/manage/index',
              component: 'manage/index'
            },
            {
              path: '/user/agent',
              component: 'user/agent'
            },
            {
              path: '/manage/userManage',
              component: 'manage/userManage'
            },
            {
              path: '/user/branchList',
              component: 'user/branchList'
            },
            {
              path: '/user/account',
              component: 'user/account'
            },
            {
              path: '/setting/settings',
              component: 'setting/settings'
            },
            {
              path: '/manage/privilege',
              component: 'manage/privilege'
            },
            {
              path: '/manage/addPrivilege',
              component: 'manage/addPrivilege'
            },
            {
              path: '/manage/roles',
              component: 'manage/roles'
            },
            {
              path: '/manage/permissionSet',
              component: 'manage/permissionSet'
            },
            {
              path: '/user/branch',
              component: 'user/branch'
            },
            {
              path: '/order/distribution',
              component: 'order/distribution'
            }
          ],
          redirect: '/manage/index'
        }]
        let routerArr = []
        componentData.forEach(item => {
          routerArr[item.path] = item
        })
        let resArr = []
        res.data.data.permissions && res.data.data.permissions.forEach(item => {
          if (routerArr[item.uri]) {
            resArr.push(routerArr[item.uri])
          }
        })
        routerData[0].children.push(...resArr)
        this.handleSubmit(routerData)
        this.profilePath = res.data.data.userInfoVO.profilePhotoPath
        this.nickname = res.data.data.userInfoVO.nickname
        let data = {
          pro: this.profilePath,
          nickname: this.nickname
        }
        window.sessionStorage.setItem('profile', JSON.stringify(data))
        let menu = setChildren(res.data.data.sets, res.data.data.permissions)
        window.sessionStorage.setItem('navMenu', JSON.stringify(menu))
        setTimeout(() => {
          this.$router.push({name: 'manage'})
        }, 1000)
      }
    })
  }
}
</script>

<style scoped>

</style>
